//
//  CYTrendViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTrendView.h"
#import "CYSegmentView.h"
#import "CYWinNumCell.h"
#import "CYTrendModel.h"
#import "CYChart.h"
#import "CYTrendSource.h"
#import "CYHeaderFooterView.h"
@interface CYTrendView()<UITableViewDelegate ,UITableViewDataSource>{
    TrendViewBlock _myblock;
}
@property (nonatomic, strong) UIScrollView *scrollview;
@property (nonatomic, strong) CYSegmentView *seg;//彩票位级选择器
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign) TrendType type;
@property (nonatomic, strong) CYTrendModel *model;
@property (nonatomic, strong) CYHUD *hud;//菊花
@property (nonatomic, strong) NSMutableArray *chartArr;
@property (nonatomic, strong) NSURLSessionTask *task;//网络请求句柄
@end
@implementation CYTrendView
- (instancetype)initWithBlock:(TrendViewBlock)block{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _myblock = block;
        _type = ShiYiXuanWuType;
        self.seg.hidden = NO;
    }
    return self;
}

- (CYSegmentView *)seg{
    if (!_seg) {
        WS(self)
        _seg = [[CYSegmentView alloc]initWithtitles:[CYTrendSource getUnitTitles:_lottery_name] size:13 selectColor:THEME_COLOR defaultSelectedIndex:0 block:^(NSUInteger index) {
            [weakSelf changChart:index];//改变走势图
        }];
        [self addSubview:_seg];
        
        [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).with.offset(0);
            make.right.mas_equalTo(self.mas_right).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, _seg.bounds.size.height));
        }];
    }
    return _seg;
}

/*懒加载*/
- (UIScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]init];
        _scrollview.showsVerticalScrollIndicator = NO;
        _scrollview.pagingEnabled = YES;
        _scrollview.delegate = self;
        _scrollview.bounces = NO;
        [self addSubview:_scrollview];
        
        [_scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(_seg.bounds.size.height, 0, 0, 0));
        }];
    }
    return _scrollview;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.bounces = YES;
        _tableview.separatorStyle = UITableViewCellSelectionStyleNone;
        [self.scrollview addSubview:_tableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(_seg.bounds.size.height);
            make.bottom.equalTo(self);
            make.left.equalTo(_scrollview);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    return _tableview;
}

//彩种名字
- (void)setLottery_name:(NSString *)lottery_name{
    if (_lottery_name != lottery_name){
        _lottery_name = lottery_name;
        _seg.titles = [CYTrendSource getUnitTitles:_lottery_name];
        _seg.currentIndex = 0;
    }
}

- (void)setLottery_id:(NSString *)lottery_id{
    if (_lottery_id != lottery_id) {
        _lottery_id = lottery_id;
        [self clearOldChartView];
    }
}

- (void)setModel:(CYTrendModel *)model{
    if (_model != model) {
        _model = model;
        if (!_tableview)
            self.tableview.hidden = NO;
        else
            [_tableview reloadData];
        [self setupSubView];
        if (_myblock)
            _myblock(_model.action_no,_model.end_time);
    }
}

- (void)setupSubView{
    for (int i = 0; i < _model.number.count; i++) {
        
        UIScrollView *subScrollView = [[UIScrollView alloc]init];
        subScrollView.scrollsToTop = YES;
        subScrollView.bounces = YES;
        subScrollView.tag = 200 + i;
        _scrollview.showsVerticalScrollIndicator = NO;
        [self.scrollview addSubview:subScrollView];
        [subScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_scrollview);
            make.bottom.mas_equalTo(self);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.left.equalTo(_scrollview).with.offset(SCREEN_WIDTH * (i + 1));
        }];
        
        UIImageView *chartView = [[UIImageView alloc] init];
        UIImage *chart= [CYChart chartWithMissArray:_model.number[i] winArr:_model.data index:i];
        chartView.image = chart;
        chartView.bounds = CGRectMake(0.0, 0.0, chart.size.width, chart.size.height);
        chartView.tag = 200 + i;
        subScrollView.contentSize = chartView.bounds.size;
        [subScrollView addSubview:chartView];
        
        [chartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0));
        }];
    }
    
    //    for (int i = 0; i < _model.number.count; i++) {
    
    
    ////       [self.chartArr addObject:[CYChart chartWithMissArray:_model.number[i] winArr:_model.data index:i]];
    ////        [CYChart chartWithMissArray:_model.number[i] winArr:_model.data index:i com]
    //        [CYChart chartWithMissArray:_model.number[i] winArr:_model.data index:i  image:^(UIImage *image) {
    //            UIScrollView *subScrollView = [_scrollview viewWithTag:200+i];
    //            UIImageView *chartView = [subScrollView viewWithTag:200+i];
    //            chartView.image = image;
    //                    chartView.bounds = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    //            subScrollView.contentSize = chartView.bounds.size;
    //
    //        }];
    //    }
    
    
    [_hud hideAnimated:YES];
    _scrollview.contentSize = CGSizeMake(SCREEN_WIDTH * (_model.number.count + 1), self.bounds.size.height - 44);
    _scrollview.contentOffset = CGPointZero;//归零
    
}

/*更改走势图*/
- (void)changChart:(NSUInteger)index{
    _tableview.hidden = (_tableview && index == 0)?NO:YES;
    _scrollview.contentOffset = CGPointMake(SCREEN_WIDTH *index, 0);
}

//清除已经存在的走势图
- (void)clearOldChartView{
    for (UIScrollView *subScrollView in [_scrollview subviews]) {
        if (subScrollView.tag >= 200) {
            [subScrollView removeFromSuperview];
            for (CYImageView *chartView in [subScrollView subviews]) {
                if (chartView.tag>= 200) {
                    [chartView removeFromSuperview];
                }
            }
        }
    }
    [self https];
}

//网络请求
- (void)https{
    NSDictionary *para = @{@"id":_lottery_id};
    _hud = [CYHUD hud];
    [_hud showAnimated:YES];
    _hud.label.text = @"正在努力加载...";
    self.task = [CYHttps POST:API_chart parameters:para success:^(id data) {
        self.model = [CYTrendModel mj_objectWithKeyValues:data];
    } failure:nil];
}

- (void)show{
    [self https];
}

#pragma - mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"CYWinNumCell";
    CYWinNumCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[CYWinNumCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.type = [CYTrendSource getType:_lottery_name];
    cell.model = _model.data[indexPath.row];
    return cell;
}

#pragma - mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 33;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 33;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [CYHeaderFooterView getHeaderOrFooterForTableiView:_lottery_name];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _seg.currentIndex = scrollView.contentOffset.x / SCREEN_WIDTH;
}

- (void)dealloc{
    [self.hud hideAnimated:YES];
    [self.task cancel];
    _task = nil;
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
